<?php


namespace App\Http\Controllers\Admin\Interaction;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use mysql_xdevapi\Exception;


class InteractionFaceController extends BaseController
{
    //添加
    /**
     * @api {post} /interaction/face 添加房间表情互动
     * @apiVersion 0.1.0
     * @apiName 添加房间表情互动
     * @apiGroup 房间互动表情管理
     *
     * @apiParam {String}  name  表情名称（必填）
     * @apiParam {Int}     status      状态值 0屏蔽 1显示 （必填）
     * @apiParam {String}  picture        图标地址（必填）
     * @apiParam {String}  special_start   特效地址起始（必填）
     * @apiParam {String}  special_running   特效地址运行（必填）
     * @apiParam {String}  special_end   特效地址结束（必填）
     * @apiParam {Int}  price   表情价格（必填）
     * @apiParam {Int}     sort        排序值（必填）
     * @apiParam {Int}     nobility_id        最低爵位id（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function add(Request $request)
    {

        $data = $request->only(['name', 'status', 'picture', 'special_start', 'special_running', 'special_end', 'price', 'sort', 'nobility_id']);
        $va = app('validator')->make($data, [
            'name' => 'required|string|max:255',//名称
            'status' => 'required|integer|in:0,1',//是否显示
            'picture' => 'required|string|max:255',//图标地址
            'special_start' => 'required|string|max:255',//特效地址起始
            'special_running' => 'string|max:255',//特效地址运行
            'special_end' => 'required|string|max:255',//特效地址结束
            'price' => 'required|integer|min:0',
            'sort' => 'required|integer|min:0',

            'nobility_id' => 'required|integer|min:0'//最低爵位id
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, getValidateMessage($va->messages()));
        }

//        try {
//            $data['show_time'] = json_decode($data['show_time'], true);
//        } catch (Exception $e) {
//            throw  new BasicException(10001, 'show_time格式错误');
//        }
//        if (empty($data['show_time']['start']) || empty($data['show_time']['running']) || empty($data['show_time']['end'])) {
//            throw  new BasicException(10001, '动画时间错误');
//        }
        //是否有爵位
        $isHaveNobility = app('model_nobility')->where('id', $data['nobility_id'])->first();
        if (empty($isHaveNobility)) {
            throw  new BasicException(10001, '没有该爵位');
        }

        $res = app('model_interaction_face')->insert($data);
        if ($res) {
            return Response::sendData(true);
        } else {
            throw  new BasicException(0, '添加失败');
        }
    }

    //修改

    /**
     * @api {put} /interaction/face 修改房间表情互动
     * @apiVersion 0.1.0
     * @apiName 添加房间表情互动
     * @apiGroup 房间互动表情管理
     *
     * @apiParam {Int}  id  表情名称（必填）
     * @apiParam {String}  name  表情名称（必填）
     * @apiParam {Int}     status      状态值 0屏蔽 1显示 （必填）
     * @apiParam {String}  picture        图标地址（必填）
     * @apiParam {String}  special_start   特效地址起始（必填）
     * @apiParam {String}  special_running   特效地址运行（必填）
     * @apiParam {String}  special_end   特效地址结束（必填）
     * @apiParam {Int}  price   表情价格（必填）
     * @apiParam {Int}     sort        排序值（必填）
     * @apiParam {Int}     nobility_id        最低爵位id（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function edit(Request $request)
    {
        $data = $request->only(['id', 'name', 'status', 'picture', 'special_start', 'special_running', 'special_end', 'price', 'sort', 'nobility_id']);
        $va = app('validator')->make($data, [
            'id' => 'required|integer|min:0',
            'name' => 'required|string|max:255',//名称
            'picture' => 'required|string|max:255',//图标地址
            'special_start' => 'required|string|max:255',//特效地址起始
            'special_running' => 'url|max:255',//特效地址运行
            'special_end' => 'required|string|max:255',//特效地址结束
            'price' => 'required|integer|min:0',
            'sort' => 'required|integer|min:0',
            'nobility_id' => 'required|integer|min:0'//最低爵位id
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, getValidateMessage($va->messages()));
        }

        //是否有爵位
        $isHaveNobility = app('model_nobility')->where('id', $data['nobility_id'])->first();
        if (empty($isHaveNobility)) {
            throw  new BasicException(10001, '没有该爵位');
        }

        $res = app('model_interaction_face')->where('id', $data['id'])->update($data);
        if ($res) {
            return Response::sendData(true);
        } else {
            throw  new BasicException(0, '编辑失败');
        }
    }

    //列表

    /**
     * @api {get} /interaction/face 音效列表
     * @apiVersion 0.1.0
     * @apiName 音效列表
     * @apiGroup 房间互动表情管理
     * @apiParam {String}                     name 名称（选填）
     * @apiParam {Int}                     page 页码（选填）
     * @apiParam {Int}                       per_page 一页行数 （选填）
     * @apiSuccessExample 成功响应
     *
     * {
     * "code": 200,
     * "data": {
     * "total": 4,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 2,
     * "name": "111111111",
     * "status": 1,
     * "picture": "12",
     * "special_start": "url1",
     * "special_running": "url1",
     * "special_end": "url1",
     * "price": "11.00",
     * "sort": 1,
     * "interval_time": 1,
     * "nobility_id": 1
     * },
     * {
     * "id": 3,
     * "name": "1111",
     * "status": 1,
     * "picture": "12",
     * "special_start": "url1",
     * "special_running": "url1",
     * "special_end": "url1",
     * "price": "11.00",
     * "sort": 1,
     * "interval_time": 1,
     * "nobility_id": 2
     * },
     * {
     * "id": 4,
     * "name": "1111",
     * "status": 1,
     * "picture": "12",
     * "special_start": "url1",
     * "special_running": "url1",
     * "special_end": "url1",
     * "price": "11.00",
     * "sort": 1,
     * "interval_time": 1,
     * "nobility_id": 2
     * },
     * {
     * "id": 5,
     * "name": "1111",
     * "status": 1,
     * "picture": "12",
     * "special_start": "url1",
     * "special_running": "url1",
     * "special_end": "url1",
     * "price": "11.00",
     * "sort": 1,
     * "interval_time": 1,
     * "nobility_id": 2
     * }
     * ]
     * }
     * }
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function list(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'page' => 'required|integer|gt:0',
                'per_page' => 'required|integer|gt:0',
                'name' => 'string|min:0'
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $name = $request->input('name', '');

        $list = app('model_interaction_face')
            ->orderBy('sort', 'desc')
            ->where('name', 'like', '%' . $name . '%')
            ->paginate($perPage, ['*'], '', $page)->toArray();

        //返回值有固定格式
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];
        return Response::sendData($result);
    }

    /**
     * @api {delete} /interaction/face 删除与批量删除
     * @apiVersion 0.1.0
     * @apiName 删除与批量删除
     * @apiGroup 房间互动表情管理
     *
     * @apiParam {String}                     ids  用","分隔（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function delete(Request $request)
    {
        $ids = $request->input('ids', null);
        $idArray = explode(',', $ids);
        if (empty($ids)) {
            throw new BasicException(10001);
        }
        $res = app('model_interaction_face')->destroy($idArray);
        if ($res == 0) {
            throw new BasicException(10001);
        }
        return Response::sendData(true);
    }

    /**
     * @api {put} /interaction/face/status 改变显示状态
     * @apiVersion 0.1.0
     * @apiName 改变显示状态
     * @apiGroup 房间互动表情管理
     *
     * @apiParam {Int}                     id  用","分隔（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function changeStatus(Request $request)
    {
        $id = $request->input('id', null);
        if (empty($id)) {
            throw new BasicException(10001);
        }
        $model = app('model_interaction_face');
        $detail = $model->where('id', $id)->first();
        if (empty($detail)) {
            throw new BasicException(10001);
        }
        if ($detail['status'] == 0) {
            $res = $model->where('id', $id)->update(['status' => 1]);
        } else {
            $res = $model->where('id', $id)->update(['status' => 0]);
        }
        if ($res) {
            return Response::sendData(true);
        } else {
            throw new BasicException(0, '修改失败');
        }
    }


}